<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 表单 -->
        <el-form
          ref="userInfoRef"
          label-width="120px"
          :model="userInfoForm"
          :rules="userInfoRules"
          style="margin-left: 120px; margin-top: 30px"
        >
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfoForm.username" style="width: 300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input
              v-model="userInfoForm.mobile"
              disabled
              style="width: 300px"
            />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfoForm.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <UploadImg :url="userInfoForm.staffPhoto" @get-url="getUrl" />
          </el-form-item>
          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="submit">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById, saveUserDetailById } from '@/api'
import UploadImg from '@/components/UploadImg'
export default {
  name: 'Detail',
  components: {
    UploadImg
  },
  data() {
    return {
      userInfoForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      userInfoRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchUserDetailById()
  },
  methods: {
    async fetchUserDetailById() {
      const res = await getUserDetailById(this.$route.query.id)
      this.userInfoForm = res.data
    },
    getUrl(imgUrl) {
      this.userInfoForm.staffPhoto = imgUrl
    },
    submit() {
      // 1. 兜底校验
      this.$refs.userInfoRef.validate(async(valid) => {
        if (!valid) return
        // 2. 发起请求
        await saveUserDetailById(this.userInfoForm)
        // 3. 提示用户
        this.$message.success('更新成功')
        // 4. 刷新数据
        // this.fetchUserDetailById()
        // 跳转
        this.$router.go(-1)
      })
    }
  }
}
</script>

<style>
</style>
